package cn.Liujialin.org.service.impl;

import cn.Liujialin.basic.util.MD5Utils;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.basic.util.StrUtils;
import cn.Liujialin.org.domain.Employee;
import cn.Liujialin.org.mapper.EmployeeMapper;
import cn.Liujialin.org.query.EmployeeQuery;
import cn.Liujialin.org.service.IEmployeeService;
import cn.Liujialin.user.domain.LoginInfo;
import cn.Liujialin.user.mapper.LoginInfoMapper;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//委托给事务管理
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    //保存
    @Override
    @Transactional
    public void save(Employee employee) {
        //初始化盐值和密码
        isinEmloyee(employee);
        //先保存到LoginInfo
        LoginInfo loginInfo = employee2LoginInfo(employee);
        //将新的loginInfo重新存储
        loginInfoMapper.save(loginInfo);
        //返回自增id
        employee.setLogininfo_id(loginInfo.getId());
        employeeMapper.save(employee);

    }

    private void isinEmloyee(Employee employee) {
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密
        String md5 = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5);
        employee.setSalt(salt);

    }

    private LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo info = new LoginInfo();
        //属性同名拷贝原则
        BeanUtils.copyProperties(employee,info);
        info.setType(0);
        return info;
    }

    //删除
    @Override
    @Transactional
    public void del(Long id) {
        //先获取到登录表id 登录表id来源于员工外键,先查询到我们需要的数据
        Employee employee = employeeMapper.findById(id);
        //从中拿出外键，交给登录删除
        loginInfoMapper.del(employee.getLogininfo_id());
        //然后再自己删除
        employeeMapper.del(id);

    }
    //修改
    @Override
    @Transactional
    public void update(Employee employee) {
        //修改盐值
        updateEmloyee(employee);
        //先把修改的数据保存到登录表Logininfo
        LoginInfo loginInfo = employee2LoginInfo(employee);
        //修改新的
        loginInfoMapper.update(loginInfo);
        employee.setLogininfo_id(loginInfo.getId());
        employeeMapper.update(employee);

    }

    private void updateEmloyee(Employee employee) {
        String randomString = StrUtils.getComplexRandomString(32);
        String byMd5 = MD5Utils.encrypByMd5(employee.getPassword() + randomString);
        employee.setSalt(randomString);
        employee.setPassword(byMd5);
    }

    //查询单个
    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }

    //查询所有
    @Override
    public List<Employee> findByAll() {
        return employeeMapper.findByAll();
    }

    //分页以及高级查询
    @Override
    public PageList<Employee> queryPage(EmployeeQuery query) {
        Long total= employeeMapper.queryCount(query);
        List<Employee> rows = employeeMapper.queryData(query);
        return new PageList<>(total,rows);

    }
    //批量删除
    @Override
    public void patchDelete(List<Long> ids) {
        employeeMapper.patchDelete(ids);
    }
}
